import utils from '../src/utils';

// 测试 isFunction 函数
describe('类型判断单元测试', () => {
    it('isFunction positive test', () => {
        const func = () => { };
        expect(utils.isFunction(func)).toBe(true);
    });

    it('isFunction negative test', () => {
        const notFunc = 'I am not a function';
        expect(utils.isFunction(notFunc)).toBe(false);
    });

    it('isString test', () => {
        expect(utils.isString('')).toBe(true)
        expect(utils.isString('null')).toBe(false)
        expect(utils.isString('undefined')).toBe(false)
    })

    it('isNumber positive test', () => {
        expect(utils.isNumber(100)).toBe(true)
    })

    it('isNumber negative test', () => {
        expect(utils.isNumber('100')).toBe(false)
    })

    it('isNull positive test', () => {
        expect(utils.isNull(null)).toBe(true)
        expect(utils.isNull("null")).toBe(true)
    })

    it('isNull nagetive test', () => {
        expect(utils.isNull(12)).toBe(false)
        expect(utils.isNull("")).toBe(false)
        expect(utils.isNull([])).toBe(false)
    })
});

describe('isPlainObject test', () => {
    it('{age:1} 是一个对象', () => {
        expect(utils.isPlainObject({age:1})).toBe(true)
    })

    it('function(){} 不是一个对象', () => {
        let f = function(){}
        expect(utils.isPlainObject(f)).toBe(false)
    })
})

describe('merge test', () => {
    it('合并对象', () => {
        const mObj = utils.merge({age: 1, sex:'f', info:{l:3, t:1}}, {age:2, name:'li', info:{l:2}, tiys:[1,2]});
        expect(mObj).toEqual({age:2, name:'li', sex:'f', info:{l:2, t:1}, tiys:[1,2]});
    })
})

describe('forEach test', () => {
    it('遍历对象操作', () => {
        let obj = {age: 1, sex:'f'}
        utils.forEach(obj, (item, key) => {
            obj[key] = item + 1;
        });
        expect(obj).toEqual({age: 2, sex:'f1'});
    })

    it('遍历数组操作', () => {
        let arr = [1,2,3,4,5]
        utils.forEach(arr, (item, i, _arr) => {
            _arr[i] = item + 2;
        });
        expect(arr).toEqual([3,4,5,6,7]);
    })
})


describe('isValidDate test', () => {
    it('is not a date', () => {
        expect(utils.isValidDate(new Date('d'))).toBe(false);
        expect(utils.isValidDate(new Date('2023/02/01'))).toBe(true);
    })
})